//
//  profileController.m
//  CCSD
//
//  Created by peterwang on 5/29/15.
//  Copyright (c) 2015 NeuroSky. All rights reserved.
//

#import "profileController.h"


 NSString* const nameKey=@"name";
 NSString* const genderKey=@"gender";
 NSString* const ageKey    =@"age";
 NSString* const heightKey=@"height";
 NSString* const weightKey=@"weight";

@interface profileController (){
    NSString *name;
    NSString *age;
    NSString *gender;
    NSString *weight;
    NSString *height;
    
    NSUserDefaults *userSetting;
}

@end

@implementation profileController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self  setupButtons];
    
    [self setupTextFiledKeyType];
    
    [self setUserDefaultOnce];
    
   
    
 //   NSLog(@"view did Load");
    
}

-(void)setUserDefaultOnce{
    userSetting=[NSUserDefaults standardUserDefaults];
    
    if (![userSetting objectForKey:nameKey]) {

        [self setString2UserDefaultOnce];
        
    }
    
}

-(void) setString2UserDefaultOnce{
    
    [userSetting setObject:@"TOM" forKey:nameKey ];
    [userSetting setObject:@"Male" forKey:genderKey ];
    [userSetting setObject:@"23" forKey:ageKey ];
    [userSetting setObject:@"175" forKey:heightKey ];
    [userSetting setObject:@"60" forKey:weightKey ];
    [userSetting synchronize];
    
}

-(void) setString2UserDefault{
    
    [userSetting setObject:name forKey:nameKey ];
    [userSetting setObject:gender forKey:genderKey ];
    [userSetting setObject:age forKey:ageKey ];
    [userSetting setObject:height forKey:heightKey ];
    [userSetting setObject:weight forKey:weightKey ];
    [userSetting synchronize];
    
}

-(void) stringFromUserDefault{
    name=[userSetting objectForKey:nameKey];
    age=[userSetting objectForKey:ageKey];
    gender=[userSetting objectForKey:genderKey];
    weight=[userSetting objectForKey:weightKey];
    height=[userSetting objectForKey:heightKey];
    

}

-(void) string2TextField{
    _nameField.text=name;
    _ageField.text=age;
    _weightField.text=weight;
    _heightField.text=height;
    
    [_genderButton setTitle:gender forState:UIControlStateNormal];
}

-(void) textField2String{
    
    name=_nameField.text;
    age=_ageField.text;
    weight=_weightField.text;
    height=_heightField.text;

    [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [age stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [weight stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [height stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

}

-(void)viewWillAppear:(BOOL)animated{

    [self stringFromUserDefault];
    [self string2TextField];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - set up views
-(void)setupTextFiledKeyType{
    _nameField.keyboardType=UIKeyboardTypeDefault;
    _ageField.keyboardType=UIKeyboardTypeNumberPad;
    _heightField.keyboardType=UIKeyboardTypeNumberPad;
    _weightField.keyboardType=UIKeyboardTypeNumberPad;
    
    _nameField.font=[UIFont fontWithName:@"Optima" size:14];
    _ageField.font=[UIFont fontWithName:@"Optima" size:14];
    _heightField.font=[UIFont fontWithName:@"Optima" size:14];
    _weightField.font=[UIFont fontWithName:@"Optima" size:14];

}

-(void)setupButtons{
    NSArray *buttonsArray=[NSArray arrayWithObjects:_cancel,_save,_genderButton,nil];
    
    for (UIButton *button in buttonsArray)
    {
        button.layer.borderColor=[UIColor blueColor].CGColor;
        button.layer.borderWidth=2;
        button.titleLabel.font=[UIFont fontWithName:@"Optima" size:14];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [_nameField resignFirstResponder];
    [_ageField resignFirstResponder];
    [_heightField resignFirstResponder];
    [_weightField resignFirstResponder];
    
    
}

#pragma mark - button action
- (IBAction)CancelAction:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)SaveAction:(id)sender {
    
    [self textField2String];
    
    [self setString2UserDefault];
    
    [self dismissViewControllerAnimated:YES completion:^{

        
    }];

}

- (IBAction)MaleAction:(id)sender {
    
    if ([gender isEqualToString:@"Female"]) {
        [_genderButton setTitle:@"Male" forState:UIControlStateNormal];
        gender=@"Male";
    }else{
        [_genderButton setTitle:@"Female" forState:UIControlStateNormal];
        gender=@"Female";
        
    }
}

@end
